{include file="header.html"}
<body class="gr">

<div class="navBar">
	<div class="titleView-x">填写订单</div>
</div>

<div class="cart-info main-top">
	<div class="view">
		<a href="{if isset($renew) && $renew==1}javascript:void(0){else}wap.php?app=cart&act=person{/if}" class="ge-bottom"><span class="text {if $data.person}text-x{/if}">{if $data.person}{$data.person}{else}请选择参保人{/if}</span><span>参保人</span></a>
		<a href="{if isset($renew) && $renew==1}javascript:void(0){else}wap.php?app=cart&act=province{/if}" class="ge-bottom"><span class="text {if $data.city}text-x{/if}">{if $data.city}{$data.city}{else}请选择参保地{/if}</span><span>参保地</span></a>
		<a href="{if isset($renew) && $renew==1}javascript:void(0){else}wap.php?app=cart&act=casy{/if}" class="ge-bottom"><span class="text text-casy {if $data.casy}text-x{/if}">{if $data.casy}{$data.casy}{else}请选择参保方案{/if}</span><span>参保方案</span></a>
		<a href="wap.php?app=cart&act=month" class="ge-bottom"><span class="text text-month {if $data.months}text-x{/if}">{if $data.months}{$data.months}{else}请选择参保月份{/if}</span><span>参保月份</span></a>
		<a href="wap.php?app=article&act=detail&id=7" class="last"><span>同意<span>《服务协议》</span></span></a>
	</div>

	<input type="hidden" id="city_name" value="{if $data.city_name}{$data.city_name}{/if}" />
	<input type="hidden" id="end_day" value="{if $data.end_day}{$data.end_day}{/if}" />
	<input type="hidden" id="month_name" />
	
	<form action="wap.php?app=cart&act=jiesuan" method="post">
	<input type="hidden" id="person_id" name="person_id" value="{if $data.person_id>0}{$data.person_id}{/if}" />
	<input type="hidden" id="province_id" name="province_id" value="{if $data.province_id>0}{$data.province_id}{/if}" />
	<input type="hidden" id="city_id" name="city_id" value="{if $data.city_id>0}{$data.city_id}{/if}" />
	<input type="hidden" id="casy_id" name="casy_id" value="{if $data.casy_id>0}{$data.casy_id}{/if}" />
	<input type="hidden" id="month" name="month" value="{if $data.month}{$data.month}{/if}" />
	<input type="hidden" id="gjj" name="gjj" value="" />
	<input type="hidden" id="gjj_price" name="gjj_price" value="" />
	<input type="hidden" id="renew" name="renew" value="{if isset($renew)}{$renew}{/if}" />
	<input type="hidden" id="new" name="new" value="{if isset($new)}{$new}{/if}" />
	</form>
	
	<div class="buttonView">
		<a href="javascript:void(0)" class="btn" id="formbtn">提交订单</a>
	</div>
	
	<div class="tip">
		<span class="ge-bottom">特别提示</span>
		<ol>
			<li>同一个身份证号码，同一个城市，只能按照初次购买的社保、公积金方案续费，不能修改社保公积金方案续费。</li>
			<li>不承接现已怀孕、重病等特殊人员的社保代缴业务。如客户隐瞒孕情、病情下单，我司无法为其办理相应的社保待遇。如有生育计划，请在孕前3个月，即生育前13个月提前购买社保。</li>
			<li>关于生育待遇的申领，我司仅承接异地分娩，生育备案，医疗报销等业务。涉及生育津贴、产假工资的申领我司概不接受。</li>
			<li>首次支付时系统会提示上传身份证正反面材料，填写好个人信息支付订单后，我司会有人跟进，如后续需要有其它材料补充，我司会再联系您。</li>
		</ol>
	</div>
</div>
{include file="footer.html"}
<script>
function setTip(){
	var city = $('#city_name').val(), end_day = $('#end_day').val(), month = $('#month_name').val();
	//if(!city.length || !month.length)return;
	//month = month.split('-');
	//month = Number(month[1]);
	$('.tip ol .city').remove();
	$('.tip ol').prepend('<li class="city">'+city.replace('市','')+'地区的社保增员截止日期为每个月的'+end_day+'号，超过此日期将不能购买当月，老客户将自动减员。</li>');
}
$(function(){
	if($.referer().length){
		$('.navBar').prepend('<a class="left" href="javascript:history.back()"><i class="return"></i></a>');
	}else{
		$('.navBar').prepend('<a class="right" href="wap.php?app=home&act=index"><i class="home"></i></a>');
	}
	{if $data.city_name && $data.end_day}setTip();{/if}
	$('#formbtn').click(function(){
		if(!$('#person_id').val().length){
			$.overloadError('请选择参保人');
			return;
		}
		if(!$('#province_id').val().length || !$('#city_id').val().length){
			$.overloadError('请选择参保地');
			return;
		}
		if(!$('#casy_id').val().length){
			$.overloadError('请选择参保方案');
			return;
		}
		if(!$('#month').val().length){
			$.overloadError('请选择参保月份');
			return;
		}
		$('form').submit();
	});
	
	
	if($('#renew').val().length && $('#city_id').val().length && $('#person_id').val().length){
		$.getJSON('api.php?app=cart&act=get_last&city_id=' + $('#city_id').val() + '&person_id=' + $('#person_id').val(), function(json){
			$('#casy_id').val(json.data.casy_id);
			$('.text-casy').addClass('text-x').html(json.data.casy_name);
			$('#gjj').val(json.data.gjj);
		});
	}

	//参保人
	pushBodyView('.view a:eq(0)', function(bodyView, link, delegate){
		bodyView.find('.list .view').on('click touchend', function(e){
			var e = e||window.event, o = e.target||e.srcElement;
			if($(o).is('.edit'))return;
			var id = $(this).attr('person-id'), name = $(this).attr('person-name');
			link.find('.text').addClass('text-x').html(name);
			delegate.find('#person_id').val(id);
			delegate.find('#province_id').val('');
			delegate.find('#city_id').val('');
			delegate.find('#casy_id').val('');
			delegate.find('#month').val('');
			delegate.find('.text:eq(1)').removeClass('text-x').html('请选择参保地');
			delegate.find('.text:eq(2)').removeClass('text-x').html('请选择参保方案');
			delegate.find('.text:eq(3)').removeClass('text-x').html('请选择参保月份');
			popBodyView();
			if($('#city_id').val().length){
				$.getJSON('api.php?app=cart&act=get_last&city_id=' + $('#city_id').val() + '&person_id=' + $('#person_id').val(), function(json){
					if(json.data){
						delegate.find('#casy_id').val(json.data.casy_id);
						delegate.find('.text-casy').addClass('text-x').html(json.data.casy_name);
						delegate.find('#gjj').val(json.data.gjj);
					}
				});
			}
		});
		pushBodyView('.navBar .right, .cart-person .edit', function(bodyView){
			var is_wx = {if isset($weixin_sign_package)}$.browser.wx{else}false{/if};
			if(!is_wx){
				$('.cart-person-edit .front, .cart-person-edit .back').ajaxupload({
					url : 'api.php?app=cart&act=idcard_pic',
					name : 'idcard_pic',
					innerFile : false,
					before : function(){
						$.overload();
					},
					callback : {
						success : function(json, status){
							if(json.error!=0){ $.overloadError(json.msg);return }
							$(this).css('background-image', 'url('+json.data+')');
							$(this).find('input').val(json.data);
						},
						error : function(xml, status, e){
							alert('Upload error\n'+e);
						}
					}
				});
			}else{
				wx.config({
					debug: false,
					appId: "{$weixin_sign_package.appId}",
					timestamp: "{$weixin_sign_package.timestamp}",
					nonceStr: "{$weixin_sign_package.nonceStr}",
					signature: "{$weixin_sign_package.signature}",
					jsApiList: [
						//所有要调用的 API 都要加到这个列表中
						'chooseImage',
						'uploadImage'
					]
				});
				wx.ready(function(){
					$('.cart-person-edit .front, .cart-person-edit .back').click(function(){
						var _this = $(this);
						wx.chooseImage({
							success : function(res){
								var localId = res.localIds; //返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
								$.overload('正在上传...');
								setTimeout(function(){
									wx.uploadImage({
										localId: localId.toString(), //需要上传的图片的本地ID，由chooseImage接口获得
										isShowProgressTips: 0, //默认为1，显示进度提示
										success: function(res){
											var serverId = res.serverId; //返回图片的服务器端ID
											$.postJSON('api.php?app=cart&act=idcard_pic', { idcard_pic:serverId, is_wx:1 }, function(json){
												if(json.error!=0){ $.overloadError(json.msg);return }
												_this.css('background-image', 'url('+json.data+')');
												_this.find('input').val(json.data);
											});
										},
										fail: function(res){
											$.overload(false);
											alert($.debug(res));
										}
									});
								}, 100);
							}
						});
					});
				});
			}
			$('.cart-person-edit .btn').click(function(){
				if(!$('.cart-person-edit #name').val().length){
					$.overloadError('请输入真实姓名');
					return false;
				}
				if(!$('.cart-person-edit #mobile').val().length){
					$.overloadError('请输入手机号码');
					return false;
				}
				if(!$('.cart-person-edit #mobile').coo('mobile')){
					$.overloadError('手机号码错误');
					return false;
				}
				if(!$('.cart-person-edit #idcard').val().length){
					$.overloadError('请输入身份证号');
					return false;
				}
				if(!$('.cart-person-edit #idcard').coo('idcardstrict')){
					$.overloadError($('.cart-person-edit #idcard').data('coo.error'));
					return false;
				}
				if(!$('.cart-person-edit #idcard_pic1').val().length){
					$.overloadError('请选择身份证正面图片');
					return false;
				}
				if(!$('.cart-person-edit #idcard_pic2').val().length){
					$.overloadError('请选择身份证背面图片');
					return false;
				}
				if(!$('.cart-person-edit #type').val().length){
					$.overloadError('请选择户籍类型');
					return false;
				}
				formBodyView(function(json){
					if(json.error!=0){ $.overloadError(json.msg);return }
					var delegate = delegateBodyView(bodyView);
					if(!delegate.length)return;
					reloadBodyView(delegate);
                    window.location.href="wap.php?app=cart&act=info";
                });
			});
			pushBodyView('.cart-person-edit .view > a', function(bodyView){
				$('.cart-person-type .view a').click(function(){
					$(this).addClass('selected').siblings().removeClass('selected');
				});
				$('.cart-person-type .btn').click(function(){
					if(!$('.cart-person-type .selected').length){
						$.overloadError('请选择户籍类型');
						return;
					}
					var type = $('.cart-person-type .selected').text();
					var delegate = delegateBodyView(bodyView);
					if(!delegate.length)return;
					delegate.find('#type').val(type);
					delegate.find('.view > a .text').addClass('text-x').html(type);
					popBodyView();
				});
			});
		});
	}, function(delegate, link){
		var bodyView = this;
		$.getJSON('api.php?app=cart&act=person', function(json){
			if(json.error!=0){ $.overloadError(json.msg);return }
			var html = '';
			$.each(json.data, function(){
				html += '<div class="view" person-id="'+this.id+'" person-name="'+this.name+'">\
					<div class="name ge-bottom">\
						<a class="edit" href="wap.php?app=cart&act=person_edit&id='+this.id+'"></a>\
						'+this.name+'\
					</div>\
					<span>手机号：'+this.mobile+'</span>\
					<span>身份证号：'+this.idcard+'</span>\
					<span>户籍类型：'+this.type+'</span>\
				</div>';
			});
			bodyView.find('.list').html(html);
			bodyView.find('.list .view').on('click touchend', function(e){
				var e = e||window.event, o = e.target||e.srcElement;
				if($(o).is('.edit'))return;
				var id = $(this).attr('person-id'), name = $(this).attr('person-name');
				link.find('.text').addClass('text-x').html(name);
				delegate.find('#person_id').val(id);
				delegate.find('#province_id').val('');
				delegate.find('#city_id').val('');
				delegate.find('#casy_id').val('');
				delegate.find('#month').val('');
				delegate.find('.text:eq(1)').removeClass('text-x').html('请选择参保地');
				delegate.find('.text:eq(2)').removeClass('text-x').html('请选择参保方案');
				delegate.find('.text:eq(3)').removeClass('text-x').html('请选择参保月份');
				popBodyView();
				if($('#city_id').val().length){
					$.getJSON('api.php?app=cart&act=get_last&city_id=' + $('#city_id').val() + '&person_id=' + $('#person_id').val(), function(json){
						if(json.data){
							delegate.find('#casy_id').val(json.data.casy_id);
							delegate.find('.text-casy').addClass('text-x').html(json.data.casy_name);
							delegate.find('#gjj').val(json.data.gjj);
						}
					});
				}
			});
		});
	});
	
	//参保地
	pushBodyView({ link:'.view a:eq(1)', click:function(){
		if(!$('#person_id').val().length){
			$.overloadError('请先选择参保人');
			return false;
		}
		var href = this.attr('href').replace('&person_id='+$('#person_id').val(), '');
		this.attr('href', href);
	}}, function(bodyView, link, delegate){
		function search(){
			var keyword = bodyView.find('#keyword').val();
			if(!keyword.length){
				bodyView.find('.view:not(.search) a').show();
			}else{
				bodyView.find('.view:not(.search) a').each(function(){
					if($(this).text().indexOf(keyword)>-1)$(this).show();
					else $(this).hide();
				});
			}
			bodyView.find('.view:not(.search) a').addClass('ge-bottom').filter(':visible:last').removeClass('ge-bottom');
		}
		bodyView.find('.search a').click(function(){
			search();
		});
		bodyView.find('.search input').onkey(function(code){
			if(code==13)search();
		});
		bodyView.find('.view:not(.search) a:visible:last').removeClass('ge-bottom');
		pushBodyView('.cart-province .view:not(.search) a', function(bodyView){
			function search(){
				var keyword = bodyView.find('#keyword').val();
				if(!keyword.length){
					bodyView.find('.view:not(.search) a').show();
				}else{
					bodyView.find('.view:not(.search) a').each(function(){
						if($(this).text().indexOf(keyword)>-1)$(this).show();
						else $(this).hide();
					});
				}
				bodyView.find('.view:not(.search) a').addClass('ge-bottom').filter(':visible:last').removeClass('ge-bottom');
			}
			bodyView.find('.search a').click(function(){
				search();
			});
			bodyView.find('.search input').onkey(function(code){
				if(code==13)search();
			});
			bodyView.find('.view:not(.search) a:visible:last').removeClass('ge-bottom');
			bodyView.find('.view:not(.search) a').click(function(){
				delegate.find('#province_id').val($(this).attr('province-id'));
				delegate.find('#city_id').val($(this).attr('city-id'));
				console.log($('#city_id').val());
				delegate.find('#city_name').val($(this).attr('city'));
				delegate.find('#end_day').val($(this).attr('end_day'));
				link.find('.text').addClass('text-x').html($(this).attr('name'));
				setTip();
				popRootBodyView();
                $.getJSON('api.php?app=cart&act=casy&city_id='+$(this).attr('city-id')+'&person_id='+$('#person_id').val(), function(json){
                    if(!$.isArray(json.data.casy) || !json.data.casy.length){
						delegate.find('#gjj').val('');
						delegate.find('#casy_id').val('');
						delegate.find('.text-casy').removeClass('text-x').html('请选择参保方案');
						return;
					}
                    var casy = json.data.casy, row = json.data.row, text = casy[0].name, hasCasy = true, gjj_price=json.data.gjj_price;
                    if(!text.length){
						text = '请选择参保方案';
						hasCasy = false;
					}
					//delegate.find('#gjj').val(row.money);
					delegate.find('#casy_id').val(casy[0].id);
					delegate.find('#gjj_price').val(gjj_price);
					delegate.find('.text-casy').html(text);
					if(hasCasy)delegate.find('.text-casy').addClass('text-x');
					else delegate.find('.text-casy').removeClass('text-x');
                });
                if($('#person_id').val().length){
	                $.getJSON('api.php?app=cart&act=get_last&city_id='+$(this).attr('city-id')+'&person_id='+$('#person_id').val(), function(json){
		                if(json.data){
			                delegate.find('#casy_id').val(json.data.casy_id);
			                delegate.find('.text-casy').addClass('text-x').html(json.data.casy_name);
			                delegate.find('#gjj').val(json.data.gjj);
	                    }
	                });
	                $.getJSON('api.php?app=cart&act=month&city_id='+$(this).attr('city-id')+'&person_id='+$('#person_id').val(), function(json){
		                if(!$.isArray(json.data.years) || !json.data.years.length){
			                delegate.find('#month').val('');
			                delegate.find('#month_name').val('');
			                delegate.find('.text-month').removeClass('text-x').html('请选择参保月份');
			                return;
		                }
		                var years = json.data.years, month = [];
		                for(var i=0; i<json.data.min_months; i++){
			                month.push(years[i].year+'-'+years[i].month);
		                }
		                delegate.find('#month').val(month.join(','));
		                delegate.find('#month_name').val(month[0]);
		                var html = month[0];
		                if(month.length>1)html = month[0]+' ~ '+month[month.length-1];
		                delegate.find('.text-month').addClass('text-x').html(html);
	                });
                }
			});
		});
	});
	
	//参保方案
	pushBodyView({ link:'.view a:eq(2)', click:function(){
		if(!$('#person_id').val().length){
			$.overloadError('请先选择参保人');
			return false;
		}
		//alert($('#city_id').val());
		if(!$('#city_id').val().length){
			$.overloadError('请先选择参保地');
			return false;
		}
		this.attr('href', 'wap.php?app=cart&act=casy');
		if (this.attr('href').indexOf('city_id')==-1) {
			this.attr('href', this.attr('href').concat('&city_id='+$('#city_id').val()));
		}
		if (this.attr('href').indexOf('person_id')==-1) {
			this.attr('href', this.attr('href').concat('&person_id='+$('#person_id').val()))
		};
	}}, function(bodyView, link, delegate){
		bodyView.find('#gjj').val(delegate.find('#gjj').val());
		if(!delegate.find('#gjj').val().length)bodyView.find('.gjj .btn').removeClass('btn-x');
		bodyView.find('#casy_money').html(bodyView.find('#casy_id').selected().attr('money'));
		bodyView.find('#casy_id').change(function(){
			bodyView.find('#casy_money').html($(this).selected().attr('money'));
		});
		if ($('#gjj_price').val()>0) {
			bodyView.find('#gjj').val($('#gjj_price').val());
		} else {
			bodyView.find('.gjj .btn').click(function(){
				var _this = $(this), gjj = bodyView.find('#gjj');
				if(_this.hasClass('btn-x')){
					_this.removeClass('btn-x');
					gjj.val('');
				}else{
					_this.addClass('btn-x');
					gjj.val(gjj.attr('min'));
				}
			});
		}
		bodyView.find('.buttonView .btn').click(function(){
			var gjj = bodyView.find('#gjj');
			if(gjj.val().length && (Number(gjj.val())<Number(gjj.attr('min')) || Number(gjj.val())>Number(gjj.attr('max')))){
				$.overloadError('公积金范围值为：'+gjj.attr('min')+'元~'+gjj.attr('max')+'元');
				return false;
			}
			delegate.find('#casy_id').val(bodyView.find('#casy_id').selected().val());
			delegate.find('#gjj').val(gjj.val());
			var text = bodyView.find('#casy_id').selected().text(), hasCasy = true;
			if(!text.length){
				text = '请选择参保方案';
				hasCasy = false;
			}
			link.find('.text').html(text);
			if(hasCasy)link.find('.text').addClass('text-x');
			else link.find('.text').removeClass('text-x');
			popBodyView();
		});
		pushBodyView({ link:'.buttonView a:eq(0)', click:function(){
			var casy_id = bodyView.find('#casy_id').selected().val(), gjj = Number(bodyView.find('#gjj').val());
			this.attr('href', this.attr('href').replace(/&casy_id=\d+/, '').replace(/&gjj=\d+/, '')+'&casy_id='+casy_id+'&gjj='+gjj);
		}}, function(bodyView){
			bodyView.find('.buttonView a').click(function(){
				popBodyView();
			});
		});
	});
	
	//参保月份
	pushBodyView({ link:'.view a:eq(3)', click:function(){
		if(!$('#person_id').val().length || !$('#city_id').val().length){
			$.overloadError('请先选择参保人与参保地');
			return false;
		}
		var href = this.attr('href').replace('&person_id='+$('#person_id').val(), '').replace('&city_id='+$('#city_id').val(), '')
			+'&person_id='+$('#person_id').val()+'&city_id='+$('#city_id').val();
		this.attr('href', href);
	}}, function(bodyView, link, delegate){
		bodyView.find('.month a[disabled="0"]').click(function(){
			if(!$(this).is(bodyView.find('.month a:eq(0)')) && !bodyView.find('.month a:eq(0)').hasClass('view-x'))return;
			if($(this).is(bodyView.find('.month a:eq(0)')) && $(this).hasClass('view-x'))return;
			var _this = $(this), index = _this.index(), parent = _this.parent();
			if(!parent.find('.view-x').length){
				_this.addClass('view-x');
			}else{
				var first = parent.find('.view-x:eq(0)'), firstIndex = first.index();
					last = parent.find('.view-x:last'), lastIndex = last.index();
				if(index==firstIndex && index==lastIndex){
					_this.removeClass('view-x');
				}else if(index==firstIndex || index==lastIndex){
					_this.removeClass('view-x');
				}else if(index<firstIndex){
					_this.addClass('view-x').nextUntil(first).addClass('view-x');
				}else if(index>lastIndex){
					last.nextUntil(_this).addClass('view-x');
					_this.addClass('view-x');
				}else{
					parent.find('.view-x').removeClass('view-x');
					first.addClass('view-x').nextUntil(_this).addClass('view-x');
					_this.addClass('view-x');
				}
			}
		});
		bodyView.find('.btn').click(function(){
			var month = [];
			bodyView.find('.month a.view-x').each(function(){
				month.push($(this).attr('year')+'-'+$(this).attr('month'));
			});
			if(!month.length){ $.overloadError('请选择参保月份');return }
			$.getJSON('api.php?app=cart&act=check_min_months', { city_id:$('#city_id').val(), person_id:$('#person_id').val(), months:month.join(',') }, function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				delegate.find('#month').val(month.join(','));
				delegate.find('#month_name').val(month[0]);
				var html = month[0];
				if(month.length>1)html = month[0]+' ~ '+month[month.length-1];
				link.find('.text').addClass('text-x').html(html);
				popBodyView();
			});
		});
	});
	
	//协议
	pushBodyView('.view a:eq(4)');
});
</script>